# -*- coding:utf-8 -*-
__author__ = 'yangqiong'

from tornado import gen
from tornado.ioloop import IOLoop
import os

from common.dbutils import  MySQLDBUtils
from common import timetool
MYSQL_APP_HOST = '47.92.112.59'
MYSQL_APP_DBNAME = 'qicheng'
MYSQL_APP_USERNAME = 'qic'
MYSQL_APP_PASSWORD = 'qic2017.,/'

mysql_conn_app_dict = dict(host=MYSQL_APP_HOST, port=3306,
                           user=MYSQL_APP_USERNAME,
                           pwd=MYSQL_APP_PASSWORD,
                           db=MYSQL_APP_DBNAME, charset='utf8',
                           max_idle_connections=10,
                           max_open_connections=100, max_recycle_sec=3)

mysql_app_pool = MySQLDBUtils(mysql_conn_app_dict)


@gen.coroutine
def insert_into_mysql(file_name, type):
    current_time = timetool.get_timestamp()
    sql = "insert into tiku_media (file_name, pubtime, type) values ('{}', {}, {})"
    yield mysql_app_pool.insert_one(sql.format(file_name, current_time, type))
    raise gen.Return(1)


@gen.coroutine
def main():
    file_list1 = os.listdir('/usr/share/nginx/static/img/tiku')
    for item in file_list1:
        yield insert_into_mysql(item, 0)
    file_list2 = os.listdir('/usr/share/nginx/static/img/analysis')
    for item in file_list2:
        yield insert_into_mysql(item, 1)
    file_list3 = os.listdir('/usr/share/nginx/static/audio')
    for item in file_list3:
        yield insert_into_mysql(item, 2)
    file_list4 = os.listdir('/usr/share/nginx/static/img/vippic')
    for item in file_list4:
        yield insert_into_mysql(item, 3)


if __name__ == "__main__":
    IOLoop.current().run_sync(main)
